<?php
	//Hospital class
	class Hospital {
		private $hospital_id;
		private $hospital_name;
		private $hospital_address;
		private $hospital_contact;

		//Note: Constructor does not set ID however insert() and Hospital::createHospital() sets ID
		//      Use Hospital::createHospital() for manipulation of Hospital
		
		//Constructor for Hospital
		//		
		//Param: name, address, contact
		function __construct($hospital_name, $hospital_address, $hospital_contact) {
			$this->setName($hospital_name);
			$this->setAddress($hospital_address);
			$this->setContact($hospital_contact);
		}
		
		//Setter for Hospital ID
		function setHospitalID($val) {
			$this->hospital_id = $val;
		}
		//Getter for Hospital ID
		function getHospitalID() {
			return $this->hospital_id;
		}		
		
		//Setter for Name
		function setName($val) {
			$this->hospital_name = $val;
		}
		//Getter for Name
		function getName() {
			return $this->hospital_name;
		}
		
		//Setter for Address
		function setAddress($val) {
			$this->hospital_address = $val;
		}
		//Getter for Address
		function getAddress() {
			return $this->hospital_address;
		}
		
		//Setter for Contact
		function setContact($val) {
			$this->hospital_contact = $val;
		}
		//Getter for Contact
		function getContact() {
			return $this->hospital_contact;
		}
		
		//Insert new Hospital info into database
		//
		//Return: User ID or O			
		function insert() {
			$db = Database::get();
			$query = "INSERT INTO " . HOSPITAL_TABLE . " 
				(hospital_name, hospital_address, hospital_contact)
				VALUES(?, ?, ?);";
			$stmt = $db->prepare($query);
			if ($stmt) {

				$hospital_name = $this->getName();
				$hospital_address = $this->getAddress();
				$hospital_contact = $this->getContact();
				
				$stmt->bind_param("sss", $hospital_name, $hospital_address, $hospital_contact);
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$hospital_id = $stmt->insert_id;
				$this->setHospitalID($hospital_id);
				$stmt->close();
				return intval($hospital_id);
			}
			return 0;
		}
		
		//Remove Hospital from database
		//
		//Return: true or false
		function remove() {
			$db = Database::get();
			$query = "DELETE FROM " . HOSPITAL_TABLE . " 
				WHERE hospital_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$hospital_id = $this->getHospitalID();
				$stmt->bind_param("d", $hospital_id);
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$stmt->close();
				return true;
			}
			return false;
		}

		//Create Hospital object given the ID by taking information from the database
		//Information taken from the database: hospital name, address, contact
		//
		//Param: hospital id
		//Return: Hospital object				
		static function createHospital($id) {
			$db = Database::get();
			$query = "SELECT * FROM " . HOSPITAL_TABLE . "
					  WHERE hospital_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $id);
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$result = $stmt->get_result();
				$hospital_row = null;
				if ($row = $result->fetch_assoc()) {
					$hospital_row = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Hospital: ' . $db->error);
			}
			
			$hospital = null;
			if ($hospital_row !== null) {
				$hospital_name = $hospital_row['hospital_name'];
				$hospital_address = $hospital_row['hospital_address'];
				$hospital_contact = $hospital_row['hospital_contact'];
				
				$hospital = new Hospital($hospital_name, $hospital_address, $hospital_contact);
				$hospital->setHospitalID($id);
				}
				
				
			return $hospital;
		}
		
		//Get the list of all the hospital
		//
		//Return: List of hospital
		static function getHospitalList() {
			$db = Database::get();
			$query = "SELECT * FROM " . HOSPITAL_TABLE . ";";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$result = $stmt->get_result();
				$hospital_list = null;
				while ($row = $result->fetch_assoc()) {
					$hospital_list[] = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Hospital: ' . $db->error);
			}
			
			return $hospital_list;
		}		

		//Get the list of all the hospital with doctor count
		//
		//Return: List of hospital with doctor count
		static function getHospitalListWithCount() {
			$db = Database::get();
			$query = "SELECT *, " . HOSPITAL_TABLE . ".hospital_id, count(" . DOCTOR_TABLE . ".user_id) as doctor_count FROM " . HOSPITAL_TABLE . "
					 LEFT JOIN " . DOCTOR_HOSPITAL_TABLE . "
					 ON " . HOSPITAL_TABLE . ".hospital_id = " . DOCTOR_HOSPITAL_TABLE . ".hospital_id
					 LEFT JOIN " . DOCTOR_TABLE . "
				     ON " . DOCTOR_TABLE . ".user_id = " . DOCTOR_HOSPITAL_TABLE . ".user_id
				   	 GROUP BY " . HOSPITAL_TABLE . ".hospital_id;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$result = $stmt->get_result();
				$hospital_list = null;
				while ($row = $result->fetch_assoc()) {
					$hospital_list[] = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Hospital: ' . $db->error);
			}
			
			return $hospital_list;
		}
		
		//Get the list of doctors of a Hospital given the hospital ID
		//
		//Param: hospital ID
		//Return: List of doctors
		static function getDoctorList($hospital_id) {
			$db = Database::get();
			$query = "SELECT " . DOCTOR_TABLE. ".user_id, 
					user_firstname, user_lastname, user_email, user_gender, user_contact, 
					doctor_regno, doctor_speciality 
					FROM " . DOCTOR_HOSPITAL_TABLE . "
					INNER JOIN " . DOCTOR_TABLE. " 
					ON " . DOCTOR_HOSPITAL_TABLE . ".user_id=" . DOCTOR_TABLE. ".user_id
					INNER JOIN " . USER_TABLE. " 
					ON " . USER_TABLE . ".user_id=" . DOCTOR_TABLE. ".user_id
					WHERE hospital_id=?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $hospital_id);
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$result = $stmt->get_result();
				$doctor_list = null;
				while ($row = $result->fetch_assoc()) {
					$doctor_list[] = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Hospital: ' . $db->error);
			}
			
			if ($doctor_list !== null) {
				for ($i = 0; $i < count($doctor_list); $i++) {
					$doctor_list[$i]['user_contact'] = base64_decode($doctor_list[$i]['user_contact']);
				}
			}
			
			return $doctor_list;
		}		
		
		//Find ID of a hospital given the name
		//
		//Param: hospital name
		//Return: hospital id
		static function findID($name) {
			$hospital_id = 0;
			$db = Database::get();
			$query = "SELECT hospital_id FROM " . HOSPITAL_TABLE . " 
					  WHERE hospital_name LIKE ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("s", $name);
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$stmt->bind_result($hospital_id);
				$stmt->fetch() or $hospital_id = 0;
				$stmt->close();
			} else {
				die('Invalid query for Hospital: ' . $db->error);
			}
			return intval($hospital_id);
		}		

		//Find name of a hospital given the ID
		//
		//Param: hospital id
		//Return: hospital name		
		static function findName($id) {
			$hospital_name = 0;
			$db = Database::get();
			$query = "SELECT hospital_name FROM " . HOSPITAL_TABLE . " 
					  WHERE hospital_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("s", $id);
				$stmt->execute() or die('Invalid query for Hospital: ' . $db->error);
				$stmt->bind_result($hospital_name);
				$stmt->fetch() or $hospital_name = 0;
				$stmt->close();
			} else {
				die('Invalid query for Hospital: ' . $db->error);
			}
			return $hospital_name;
		}				

		//Unattach Hospital from all Users
		//
		//Return: true or false
		function unattachFromUsers() {
			$db = Database::get();
			$query = "DELETE FROM " . DOCTOR_HOSPITAL_TABLE . " 
				WHERE hospital_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$hospital_id = $this->getHospitalID();
				
				$stmt->bind_param("d", $hospital_id);
				$stmt->execute() or die('Invalid query for Doctor Hospital: ' . $db->error);
				$stmt->close();
				
				return true;
			}
			return false;
		}		
		
	}
?>